<template>
	<om-formsub-outer :index="index" :deleteBtn="deleteBtn" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad,getFormFields, getEditorTitle,changeEditor,configSelectData,hasValue} from '@/static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				month1CacheKey:'obj-year-tinstAnnualTargetPlan-title_work-month1-cache',
				monthCacheKey:'obj-year-tinstAnnualTargetPlan-title_work-month-cache',
				month2CacheKey:'obj-year-tinstAnnualTargetPlan-title_work-month2-cache',
				monthCacheKey1:'obj-year-tinstAnnualTargetPlan-title_work-month-cache1',
				dutyOffCache:"obj-year-tinstAnnualTargetPlan-title_work-dutyOff-cache",
				evaOffCache:"obj-year-tinstAnnualTargetPlan-title_work-evaOff-cache",
				indexCache:"obj-year-tinstAnnualTargetPlan-title_index-evaOff-cache",
				index1Cache:"obj-year-tinstAnnualTargetPlan-title_index1-evaOff-cache",
				busCode:'',
				canEdit:true,
				deleteBtn:true,
				evaMultiOffCacheKey:'obj-year-tinstAnnualTargetPlan-title_index1-evaMultiOff-cache',
				defaultDataCache:'obj-year-tinstAnnualTargetPlan-title_index1-evaOffdefaultData-cache'
			})
		},
		methods:{
			inputMaxScore(val){
				let _that = this
				if(Number(val) > 10){
					_that.$refs.autoform.formdata.maxScore = 10
				}
				if(Number(val) < 0){
					_that.$refs.autoform.formdata.maxScore = 0
				}
				if( Number(val) >= 0 &&  Number(val)<= 10){
					_that.$refs.autoform.formdata.maxScore = Number(val).toFixed(1)
				}
				if(!val){
					_that.$refs.autoform.clear("title_work","maxScore")
				}
			},
			clearDutyOffId(){
				let _that = this
				_that.$refs.autoform.clear("title_work","dutyOffId")
				_that.clearEvaMultiOffId()
			},
			clearEvaMultiOffId(){
				let _that = this
				_that.$refs.autoform.clear("title_work","evaMultiOffId")
				uni.removeStorage({
					key:_that.defaultDataCache
				})
			},
			getMultiEvaUserResponse(userIdPosId){
				let _that = this
				_that.om.request(
					"workEvaluateConfig/getMultiEvaUserResponse",
					{
						busCode:_that.busCode,
						userIdPosId: userIdPosId
					}
				).then(res=>{
					if(_that.canEdit){
						let objArr = []
						let ids = res.value.evaMultiOffId.split(',')
						let names = res.value.evaMultiOffName.split(',')
						let posNames = res.value.evaMultiPosName.split(',')
						for(let index in ids){
							let obj = {
								checked: true,
								id: null,
								instId: null,
								instName: '',
								isUser: true,
								posId: null,
								posInstName:'',
								posName: '',
								username: ''
							}
							let userPosId = ids[index].split('/')
							obj.id = userPosId[0]
							obj.posId = userPosId[1]
							obj.username = names[index]
							obj.posName = posNames[index]
							objArr.push(obj)
						}
						_that.$refs.autoform.formdata.evaMultiPosName = res.value.evaMultiPosName
						uni.setStorage({
							key:_that.defaultDataCache,
							data:objArr
						})
					}
					_that.$refs.autoform.formdata.evaMultiOffId = res.value.evaMultiOffId
					_that.$refs.autoform.formdata.evaMultiOffName = res.value.evaMultiOffName
				})
			},
			inutMaxScore(val){
				let _that = this
				if(Number(val) > 10){
					_that.$refs.autoform.formdata.maxScore = 10
				}
				if(Number(val) < 0){
					_that.$refs.autoform.formdata.maxScore = 0
				}
				if( Number(val) >= 0 &&  Number(val)<= 10){
					_that.$refs.autoform.formdata.maxScore = Number(val).toFixed(1)
				}
				if(!val){
					_that.$refs.autoform.clear("title_work","maxScore")
				}
			},
			onInputScore(value){
				let _that = this
				if(Number(value) >10){
					_that.$refs.autoform.formdata.maxScore = 10
				}else if(Number(value)<1){
					_that.$refs.autoform.formdata.maxScore = 1
				}
			},
			getMonthList(){
				let _that  = this
				_that.om.request(
					"dictZero/getComboTree",
					{
						code:"38283"
					}
				).then(res=>{
					let monthList = []
					for(let item of res){
						let obj = JSON.parse(JSON.stringify(item))
						delete obj.children
						monthList.push(obj)
						if(item.children){
							for(let sub of item.children){
								let obj1 = JSON.parse(JSON.stringify(sub))
								delete obj1.children
								monthList.push(obj1)
							}
						}
					}
					uni.setStorage({
						key: _that.month1CacheKey,
						data:monthList,
						success: (res) => {
							let title = _that.$refs.autoform.getEditorTitle("title_work", "startMonthId")
							_that.$refs.autoform.changeEditor("title_work","startMonthId",{
								link:"../../public/search/search",
								"linkParam":{
									other:JSON.stringify({
									}),
									namefield:"text",
									initload:true,
									defaultData: true,
									otherCache: _that.month1CacheKey,
									cachename:_that.monthCacheKey,
									placeholder: "请输入" + title
								}
							})
						}
					})
					uni.setStorage({
						key: _that.month2CacheKey,
						data:monthList,
						success: (res) => {
							let title = _that.$refs.autoform.getEditorTitle("title_work", "endMonthId")
							_that.$refs.autoform.changeEditor("title_work","endMonthId",{
								link:"../../public/search/search",
								"linkParam":{
									other:JSON.stringify({
									}),
									namefield:"text",
									initload:true,
									defaultData: true,
									otherCache: _that.month2CacheKey,
									cachename:_that.monthCacheKey1,
									placeholder: "请输入" + title
								}
							})
						}
					})
				})
			}
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.dutyOffCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.dutyOffId = d.id
							formdata.dutyOffName = d.userPosName
							_that.getMultiEvaUserResponse(d.userId+'/'+d.posId)
							uni.removeStorage({
								key:_that.dutyOffCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.evaMultiOffCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data;
							uni.setStorage({
								key:_that.defaultDataCache, 
								data:d
							})
							let formdata = _that.$refs.autoform.formdata;
							if(d.length){
								let idList = []
								let nameList = []
								let posNameList = []
								for(let index in d){
									idList.push(d[index].id + '/' + d[index].posId)
									nameList.push(d[index].username)
									posNameList.push(d[index].posName)
								}
								
								let idStr = idList.map(e =>e).join(",")
								let nameStr = nameList.map(e =>e).join(",");
								let posNameStr = posNameList.map(e =>e).join(",");
								
								formdata.evaMultiOffId = idStr
								formdata.evaMultiOffName = nameStr
								formdata.evaMultiPosName = posNameStr
							}else{
								_that.$refs.autoform.clear("title2","evaMultiOffId")
							}
							uni.removeStorage({
								key: _that.evaMultiOffCacheKey
							});
						}
					}
				})
				uni.getStorage({
					key:_that.evaOffCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.evaOffId = d.id
							formdata.evaOffName = d.userName
							uni.removeStorage({
								key:_that.evaOffCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.monthCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.startMonthId = d.id
							formdata.startMonthName = d.text
							uni.removeStorage({
								key:_that.monthCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.indexCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.indexCode = d.orderNum
							uni.removeStorage({
								key:_that.indexCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.monthCacheKey1,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.endMonthId = d.id
							formdata.endMonthName = d.text
							uni.removeStorage({
								key:_that.monthCacheKey1
							})
						}
					}
				})
			}
		},
		onUnload() {
			let _that = this
			uni.removeStorage({
				key:_that.defaultDataCache
			})
		},
		onLoad(e) {
			let _that = this
			let arr = [
				{
					url:"dataDict/getIntByCode",
					param:{
						code:'DD021025001'
					}
				}
			]
			subOnLoad(_that, e, function(ajaxData) {
			if(e.configDataCacheKey){
				if(e.busCode){
					_that.busCode = e.busCode
				}
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							let objFormCode = JSON.parse(uni.getStorageSync('objFormCode')) 
								changeEditor("title_work","orderNum",{
									defaultName:objFormCode.DICT_CODE_N+(_that.order),
									defaultValue:objFormCode.DICT_CODE_N+(_that.order)
								},null,_that.autoformdata)
								changeEditor("title_work","maxScore",'max',10,_that.autoformdata)
								changeEditor("title_work","dutyOffId",{
									link:"../../public/search/search",
									"linkParam":{
										other:JSON.stringify({
											instId: e.instId
										}),
										url:"tinstAnnualTargetPlan/getUserOffList",
										namefield:urlUtil.userPageUrl().namefield,
										initload:true,
										cachename:_that.dutyOffCache
									}
								},null,_that.autoformdata)
								changeEditor("title_work","evaOffId",{
									link:"../../public/search/search",
									"linkParam":{
										other:JSON.stringify({
										}),
										url:"orgInstPosUserOfficial/findListByOrgId",
										namefield:urlUtil.userPageUrl().namefield,
										initload:true,
										cachename:_that.evaOffCache
									}
								},null,_that.autoformdata)
								if(ajaxData[0].value + '' == '0'){
									changeEditor("title_work","evaMultiOffId", {
										'link':'../../public/searchUser/searchUser',
										linkParam: {
											cachename: _that.evaMultiOffCacheKey,
											defaultDataCache:_that.defaultDataCache,
											isNew:true,
											isUserPos:true,
											placeholder: urlUtil.userPageUrl().prompt,
											pagination: true
										}
									}, null, _that.autoformdata);
								}else{
									_that.canEdit = false
									changeEditor("title_work","evaMultiOffId",'type','labelbox',_that.autoformdata)
								}
								if(_that.formdata.evaMultiOffId && (ajaxData[0].value + '' == '0')){
									let objArr = []
									let ids = _that.formdata.evaMultiOffId.split(',')
									let names = _that.formdata.evaMultiOffName.split(',')
									let posNames = _that.formdata.evaMultiPosName.split(',')
									for(let index in ids){
										let obj = {
											checked: true,
											id: null,
											instId: null,
											instName: '',
											isUser: true,
											posId: null,
											posInstName:'',
											posName: '',
											username: ''
										}
										let userPosId = ids[index].split('/')
										obj.id = userPosId[0]
										obj.posId = userPosId[1]
										obj.username = names[index]
										obj.posName = posNames[index]
										objArr.push(obj)
									}
									uni.setStorage({
										key:_that.defaultDataCache,
										data:objArr
									})
								}
								uni.setStorage({
									key: _that.index1Cache,
									data:JSON.parse(e.indexList),
									success: (res) => {
										let title = getEditorTitle("title_work", "indexCode",_that.autoformdata)
										changeEditor("title_work", "indexCode", {
											link: "../../public/search/search",
											"linkParam": {
												other: JSON.stringify({}),
												cachename: _that.indexCache,
												otherCache: _that.index1Cache,
												defaultData: true,
												initload: true,
												namefield: "orderNum",
												extrafield: 'indexName',
												placeholder: "请输入" + title
											}
										},null,_that.autoformdata)
									}
								})
								if(_that.formdata.upperFlag){
									_that.deleteBtn = false
									let arr = getFormFields(_that.autoformdata)
									for(let index in arr){
										changeEditor("title_work",arr[index],'type','labelbox',_that.autoformdata)
									}
									setTimeout(()=>{
										_that.om.toast('当前工作项为上级分解工作项，禁止修改',2000)
									},500)
								}
								if(_that.formdata.kpiFlag){
									_that.deleteBtn = false
									let arr = getFormFields(_that.autoformdata)
									for(let index in arr){
										changeEditor("title_work",arr[index],'type','labelbox',_that.autoformdata)
									}
									setTimeout(()=>{
										_that.om.toast('当前指标为kpi配置指标，仅做显示，禁止修改',2000)
									},500)
								}
							}
						},
					})
				}
				_that.$nextTick(function(){
					if(!_that.$refs.autoform.formdata.maxScore){
						_that.$refs.autoform.formdata.maxScore = 10
					}
					_that.getMonthList()
				})			
			},arr)
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                         
